GetMeanFloat Function

private function GetMeanFloat(vector, nodata) result(mean)

compute mean of a vector of real numbers. If nodata is passed numbers are filetered before computing mean

Arguments

Type IntentOptional Attributes Name
real(kind=float), intent(in) :: vector(:)
real(kind=float), intent(in), optional :: nodata

Return Value real(kind=float)


Variables

Type Visibility Attributes Name Initial
real(kind=float), public :: count
integer(kind=short), public :: i

Source Code

FUNCTION GetMeanFloat &
!
(vector, nodata) &
!
RESULT (mean)

IMPLICIT NONE

!Arguments with intent(in):
REAL (KIND = float), INTENT(IN) :: vector (:)
REAL (KIND = float), OPTIONAL, INTENT(IN) :: nodata


!Local declarations:
REAL (KIND = float) :: mean
REAL (KIND = float) :: count
INTEGER (KIND = short) :: i

!---------------------------end of declarations--------------------------------
mean = 0.
count = 0.

!cumulate
DO i = 1, SIZE(vector)
  IF ( PRESENT(nodata) ) THEN
    IF (vector(i) /= nodata) THEN
       mean = mean + vector (i)
       count = count + 1.
    END IF
  ELSE
    mean = mean + vector (i)
    count = count + 1.
  END IF
END DO

!compute mean
IF ( PRESENT(nodata) ) THEN 
  IF (count == 0.) THEN
     mean = 0.
     CALL Catch ('warning', 'Statistics',  &
        'calculate mean: no valid numbers in vector: ', argument = &
        'mean set to zero' )
  ELSE
     mean = mean / count
  END IF
ELSE
   mean = mean / count
END IF


RETURN
END FUNCTION GetMeanFloat